<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="./js/vue.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" />
    <title>Document</title>
    <style>
        .zoom-enter-active {
            animation: bounceInRight 1s;
        }

        .zoom-leave-active {
            animation: backOutLeft 1s;
        }
    </style>
</head>

<body>
    <div id="app">
        <button @click="flag=!flag">点击切换</button>
        <select name="" id="" v-model="cname">
            <option value="mytable">表格</option>
            <option value="mylist">列表</option>
        </select>
        <mytable :cars="cars" v-if="flag"></mytable>
        <mylist :cars="cars" v-else></mylist>
        <transition name="zoom" mode="out-in">
            <keep-alive include="">
                <component :is="cname" :cars="cars"></component>
            </keep-alive>
        </transition>
        <myslot :title="title">
            <h2>{{title}}</h2>
            <h4>{{title}}</h4>
        </myslot>
    </div>
    <template id="mytable">
        <div>
            <table>
                <thead>
                    <tr>
                        <td>序号</td>
                        <td>品牌</td>
                        <td>型号</td>
                        <td>颜色</td>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="(v,k,index) in cars" :key="index">
                        <td>{{index+1}}</td>
                        <td>{{v.brand}}</td>
                        <td>{{v.model}}</td>
                        <td>{{v.color}}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </template>
    <template id="mylist">
        <div>
            <ul>
                <li v-for="(v,k,index) in cars" :key="index">
                    <p>{{index+1}}</p>
                    <p>{{v.brand}}</p>
                    <p>{{v.model}}</p>
                    <p>{{v.color}}</p>
                </li>
            </ul>
        </div>
    </template>
    <template id="myslot">
        <h2>
            <slot>这是一个默认插槽信息</slot>
        </h2>
    </template>
</body>
<script>
    Vue.component('mytable', {
        template: "#mytable",
        props: ['cars'],
        activated() {
            console.log("激活")
        },
        deactivated() {
            console.log('隐藏')
        }
    })
    Vue.component('mylist', {
        template: "#mylist",
        props: ['cars']
    })
    Vue.component('myslot', {
        template: "#myslot"
    })
    var cars = {
        car01: { brand: "Ford", model: "Mustang", color: "red" },
        car02: { brand: "Fiat", model: "500", color: "white" },
        car03: { brand: "Volvo", model: "XC90", color: "black" }
    }
    const vm = new Vue({
        // el:"#app",
        components: {
        },
        data: {
            title: "Vue 组件切换和插槽 (组件切换 - keep-alive ) ",
            msg: "小型、独立和通常可复用的组件构建大型应用",
            cars,
            flag: true,
            cname: "mytable"
        },
        mounted() {
            document.getElementsByTagName('title')[0].innerHTML = this.title
        }
    }).$mount("#app")
</script>

</html>